import 'dart:convert';

import 'package:flutter_test/flutter_test.dart';
import 'package:im_app/im/dao/ImGroupDao.dart';
import 'package:im_app/im/dao/ImMessageDao.dart';
import 'package:im_app/im/dao/ImSessionDao.dart';
import 'package:im_app/im/pojo/ImPojo.dart';
import 'package:im_app/im/util/DateUtil.dart';
import 'package:im_app/im/util/DbUtil.dart';
import 'package:im_app/im/util/ImSingleton.dart';
import 'package:intl/intl.dart';
import 'package:logger/logger.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

void main() {
	var _logger = Logger(
		printer: PrettyPrinter(),
	);

	test("show messageGroups 10000", () async {
		var database = await initSqlite();
		var sessionDao = ImSessionDao();
		var messageDao = ImMessageDao();
		List<ImSession> sessions = await sessionDao.getByUserIdAndSize("test00120", 10000);
		for (var session in sessions) {
			_logger.i("session ${jsonEncode(session)}");
			var id = session.id;
			if (id != null) {
				List<ImMessage> messages = await messageDao.getBySessionId(id, DateTime.now(), 100);
				for (var mg in messages) {
					_logger.i("messages ${jsonEncode(mg)}");
				}
				_logger.i("messages size ${messages.length}");
			}

		}
		_logger.i("messageGroups size ${sessions.length}");
		database.close();
	});

	test("show messages by messageGroupId", () async {
		var database = await initSqlite();
		var messageDao = ImMessageDao();
		var date = DateTime.now();
		date = date.add(const Duration(days: -10));
		List<ImMessage> messages = await messageDao.getBySessionId(100, date, 100);
		for (var mg in messages) {
		_logger.i("messages " + jsonEncode(mg));
		}
		_logger.i("messages size " + messages.length.toString());
		database.close();
	});

	test("show messages2", () async {
		var database = await initSqlite();
		var messageDao = new ImMessageDao();
		List<ImMessage> messages = await messageDao.getNewMessages(1000);
		for (var message in messages) {
		_logger.i("messages ${jsonEncode(message)}");
		}
		_logger.i("messages size ${ messages.length}");
		database.close();
	});

	test("show groups", () async {
		var database = await initSqlite();
		var groupDao = new ImGroupDao();
		List<ImGroup> groups = await groupDao.getByUserAndSize('test00120', 1000);
		for (var mg in groups) {
		_logger.i("groups " + jsonEncode(mg));
		}
		_logger.i("groups size " + groups.length.toString());
		database.close();
	});
}

Future<Database> initSqlite() async {
	sqfliteFfiInit();
	databaseFactory = databaseFactoryFfi;
	Database database = await DbUtil.connect("im.db");
	return database;
}